﻿using System;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace HobSoft.SoccerBet2014.Client.ViewModels
{
    using dm = Domain.Models;
    [Serializable]
    [XmlRoot("game")]
    public class Game : ViewModelBase
    {
        private int _bfEventID;
        [XmlAttribute("bf-id")]
        public int BfEventID
        {
            get { return _bfEventID; }
            set
            {
                PropertyValueChange(ref _bfEventID, value, "BfEventID");
            }
        }

        private string _league;
        [XmlElement("league")]
        public string League
        {
            get { return _league; }
            set
            {
                PropertyValueChange(ref _league, value, "League");
            }
        }

        private string _homeTeam;
        [XmlElement("home-team")]
        public string HomeTeam
        {
            get { return _homeTeam; }
            set
            {
                PropertyValueChange(ref _homeTeam, value, "HomeTeam");
            }
        }

        private string _awayTeam;
        [XmlElement("away-team")]
        public string AwayTeam
        {
            get { return _awayTeam; }
            set
            {
                PropertyValueChange(ref _awayTeam, value, "AwayTeam");
            }
        }

        private byte _homeScore;
        [XmlElement("home-score")]
        public byte HomeScore
        {
            get { return _homeScore; }
            set
            {
                PropertyValueChange(ref _homeScore, value, "HomeScore");
            }
        }

        private byte _awayScore;
        [XmlElement("away-score")]
        public byte AwayScore
        {
            get { return _awayScore; }
            set
            {
                PropertyValueChange(ref _awayScore, value, "AwayScore");
            }
        }

        private byte _homeRed;
        [XmlElement("home-red")]
        public byte HomeRed
        {
            get { return _homeRed; }
            set
            {
                PropertyValueChange(ref _homeRed, value, "HomeRed");
            }
        }

        private byte _awayRed;
        [XmlElement("away-red")]
        public byte AwayRed
        {
            get { return _awayRed; }
            set
            {
                PropertyValueChange(ref _awayRed, value, "AwayRed");
            }
        }

        private dm.Game.GameState _gameState;
        [XmlElement("game-state")]
        public dm.Game.GameState GameState
        {
            get { return _gameState; }
            set
            {
                PropertyValueChange(ref _gameState, value, "GameState");
            }
        }

        private string _timeState;
        [XmlElement("time-state")]
        public string TimeState
        {
            get { return _timeState; }
            set
            {
                PropertyValueChange(ref _timeState, value, "TimeState");
            }
        }

        private byte? _pried;
        [XmlElement("pried")]
        public byte? Pried
        {
            get { return _pried; }
            set
            {
                PropertyValueChange(ref _pried, value, "Pried");
            }
        }

        private byte? _minutes;
        [XmlElement("minutes")]
        public byte? Minutes
        {
            get { return _minutes; }
            set
            {
                PropertyValueChange(ref _minutes, value, "Minutes");
            }
        }

        private DateTime _startTime;
        [XmlElement("start-time")]
        public DateTime StartTime
        {
            get { return _startTime; }
            set
            {
                PropertyValueChange(ref _startTime, value, "StartTime");
            }
        }

        private long _version;
        [XmlElement("version")]
        public long Version
        {
            get { return _version; }
            set
            {
                PropertyValueChange(ref _version, value, "Version");
            }
        }

        private byte _updateSourceBm;
        [XmlIgnore]
        public byte UpdateSourceBm
        {
            get { return _updateSourceBm; }
            set
            {
                PropertyValueChange(ref _updateSourceBm, value, "UpdateSourceBm");
            }
        }

        public void CheckTimeState()
        {
            byte? pried, minutes;
            CheckTimeState(UpdateSourceBm, TimeState, out pried, out minutes);
            if (pried.HasValue)
                Pried = pried;
            if (minutes.HasValue)
                Minutes = minutes;
        }

        public void CheckTimeState(byte bmId, string timeState, out byte? pried, out byte? minutes)
        {
            pried = null;
            minutes = null;
            switch (bmId)
            {
                case 10:
                    var match10 = reg10Time.Match(timeState);
                    if (match10.Success)
                    {
                        byte checkPried = 0;
                        byte checkMin = 0;
                        if (byte.TryParse(match10.Groups["p"].Value, out checkPried))
                            pried = checkPried;
                        if (byte.TryParse(match10.Groups["m"].Value, out checkMin))
                            minutes = checkMin;
                    }
                    break;
                case 11:
                    var match11 = reg10Time.Match(timeState);
                    if (match11.Success)
                    {
                        byte checkPried = 0;
                        byte checkMin = 0;
                        if (byte.TryParse(match11.Groups["p"].Value, out checkPried))
                            pried = checkPried;
                        if (byte.TryParse(match11.Groups["m"].Value, out checkMin))
                            minutes = checkMin;
                    }
                    break;
                case 12:
                    break;
                default:
                    break;
            }
        }

        private Regex reg10Time = new Regex(@"(?<p>\d)(?<t>[H|E])\s(?<m>\d{1,2})'", RegexOptions.Compiled);
        private Regex reg11Time = new Regex(@"(?<p>\d)(?<t>[H|E])\s(?<m>\d{1,2})'", RegexOptions.Compiled);
    }
}
